
package com.jf.cloud.live.controller.platform;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.CharUtil;
import com.jf.cloud.live.param.LiveUsableNumParam;
import com.jf.cloud.live.constant.LiveConstant;
import com.jf.cloud.live.constant.LiveInterfaceType;
import com.jf.cloud.live.constant.LiveRoomStatusType;
import com.jf.cloud.live.model.LiveRoom;
import com.jf.cloud.live.service.LiveLogService;
import com.jf.cloud.live.service.LiveRoomBackService;
import com.jf.cloud.live.service.LiveRoomService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;


/**
 * 直播间
 *
 * @author zz
 * @date 2022-08-17 17:09:06
 */
@RestController("platformLiveRoomController")
@RequestMapping("/p/live_room")
@Tag(name = "平台端直播间接口")
public class LiveRoomController {

    @Autowired
    private LiveRoomService liveRoomService;

    @Autowired
    private  LiveRoomBackService liveRoomBackService;

    @Autowired
    private LiveLogService liveLogService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取直播间列表" , description = "分页获取直播间列表")
    public ServerResponseEntity<PageVO<LiveRoom>> page(@Valid PageDTO pageDTO, @ParameterObject LiveRoom liveRoom) {
        liveRoom.setShopId(Constant.PLATFORM_SHOP_ID);
        PageVO<LiveRoom> liveRoomPage = liveRoomService.page(pageDTO, liveRoom);
        return ServerResponseEntity.success(liveRoomPage);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "获取直播间" , description = "根据id获取直播间")
    @Parameter(name = "id", description = "直播间id" , required = true)
    public ServerResponseEntity<LiveRoom> getById(@ParameterObject @RequestParam Long id) {
        LiveRoom liveRoomDb = liveRoomService.getById(id);
        return ServerResponseEntity.success(liveRoomDb);
    }

    @PutMapping("/toTop/{id}")
    @Operation(summary = "通过id置顶直播间")
    @Parameter(name = "id", description = "直播间id" , required = true)
    public ServerResponseEntity<Boolean> toTopRoom(@PathVariable Long id) {
        LiveRoom liveRoom = liveRoomService.getById(id);
        int count = liveRoomService.countTopRoom();
        if (count >= Constant.MAX_TOP_NUM && Objects.equals(liveRoom.getRoomTop(), 0)) {
            // 置顶已达上限10个无法进行置顶，请先取消其余直播间的置顶
            throw new LuckException("置顶已达上限10个无法进行置顶，请先取消其余直播间的置顶");
        }
        liveRoom.setRoomTop(Objects.equals(liveRoom.getRoomTop(), 0) ? 1 : 0);
        liveRoomService.update(liveRoom);
        return ServerResponseEntity.success();
    }


    @PostMapping
    @Operation(summary = "保存直播间" , description = "保存直播间")
    public ServerResponseEntity<Void> save(@Valid @RequestBody LiveRoom liveRoom) {
        if (CharUtil.length(liveRoom.getName()) < LiveConstant.ROOM_NAME_MIN_LENGTH || CharUtil.length(liveRoom.getName()) > LiveConstant.ROOM_NAME_MAX_LENGTH) {
            // 直播间名称长度非法,最短3个汉字，最长17个汉字！
            throw new LuckException("直播间名称长度非法,最短3个汉字，最长17个汉字！");
        }
        if (CharUtil.length(liveRoom.getAnchorName()) < LiveConstant.USER_NAME_MIN_LENGTH || CharUtil.length(liveRoom.getAnchorName()) > LiveConstant.USER_NAME_MAX_LENGTH) {
            // 主播昵称长度非法，最短2个汉字，最长15个汉字
            throw new LuckException("主播昵称长度非法，最短2个汉字，最长15个汉字");
        }
        liveRoom.setShopId(AuthUserContext.get().getTenantId());
        liveRoom.setApplyTime(new Date());
        liveRoom.setLiveStatus(LiveRoomStatusType.NO_START.value());
        liveRoom.setId(null);
        liveRoom.setUpdateTime(new Date());
        liveRoomService.save(liveRoom);
        return ServerResponseEntity.success();
    }


    @GetMapping("/getAddRoomNum")
    @Operation(summary = "查询商家今日剩余可用添加直播间次数")
    public ServerResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.CREATE_ROOM);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.REMOVE_LIVE_ROOM);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ServerResponseEntity.success(liveNum);
    }


    @DeleteMapping
    @Operation(summary = "删除直播间" , description = "根据直播间id删除直播间")
    public ServerResponseEntity<Boolean> delete(@RequestParam Long id) throws WxErrorException {
        return ServerResponseEntity.success(liveRoomService.removeRoomById(id, Constant.PLATFORM_SHOP_ID));
    }

    @GetMapping("/getRemoveRoomNum")
    @Operation(summary = "查询平台今日剩余可用添加直播间次数")
    public ServerResponseEntity<LiveUsableNumParam> getRemoveRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.REMOVE_LIVE_ROOM);
        return ServerResponseEntity.success(liveNum);
    }
}
